<template>
  <view class="router-view bcl-active container">
    <view>
      <view class="fz-15 bold mb-15">输入您的旧密码</view>

      <view class="uni-input bcl-white">
        <input
          type="password"
          placeholder="请输入旧密码"
          :focus="true"
          v-model="passwordForm.oldPass"
        />
      </view>
    </view>

    <view>
      <view class="fz-15 bold mb-15">输入您的新密码</view>

      <view class="uni-input bcl-white">
        <input
          type="password"
          placeholder="请输入新密码"
          v-model="passwordForm.password"
        />
      </view>
    </view>

    <view>
      <view class="fz-15 bold mb-15">请重复您的新密码</view>

      <view class="uni-input bcl-white">
        <input
          type="password"
          placeholder="请重复您的新密码"
          v-model="passwordForm.repassword"
        />
      </view>
    </view>

    <button
      class="uni-button uni-button-lg uni-button-full mt-50"
      @tap="editPassword"
    >
      修改
    </button>
  </view>
</template>

<script>
import { updatePassword, updatePayCode } from "@/apis/user";

export default {
  data() {
    return {
      passwordForm: {
        oldPass: "",
        password: "",
        repassword: "",
      },
      isPayPassword: false,
    };
  },

  onLoad(options) {
    this.isPayPassword = options.payPassword == 1;
  },

  methods: {
    /**
     * 修改密码
     */
    async editPassword() {
      let { oldPass, password, repassword } = this.passwordForm;
      if (!oldPass || !password || !repassword)
        return this.$toast("请填写完整密码信息");
      if (password.length < 6 || repassword.length < 6)
        return this.$toast("密码长度不能小于6位");
      if (password !== repassword) return this.$toast("两次密码输入不一致");

      // options.payPassword = 1 为修改支付密码
      const updateFn = this.isPayPassword ? updatePayCode : updatePassword;

      let { code, data, message } = await updateFn(this.passwordForm);
      if (code === 200) {
        this.$toast(
          `修改成功，下次需要用新密码进行${
            this.isPayPassword ? "支付验证" : "登录"
          }`
        );
        setTimeout(uni.navigateBack, 1000);
      }
    },
  },
};
</script>
